package com.haoyun.mirage.user.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.haoyun.common.core.BaseException;
import com.haoyun.common.utils.Query;
import com.haoyun.mirage.user.mapper.SysRoleDeptMapper;
import com.haoyun.mirage.user.mapper.SysRoleMapper;
import com.haoyun.mirage.user.mapper.SysRoleMenuMapper;
import com.haoyun.mirage.user.mapper.SysUserRoleMapper;
import com.haoyun.mirage.user.model.dto.RoleDTO;
import com.haoyun.mirage.user.model.entity.SysRole;
import com.haoyun.mirage.user.model.entity.SysRoleDept;
import com.haoyun.mirage.user.model.entity.SysRoleMenu;
import com.haoyun.mirage.user.model.entity.SysUserRole;
import com.haoyun.mirage.user.service.ISysRoleService;
import com.xiaoleilu.hutool.bean.BeanUtil;
import com.xiaoleilu.hutool.lang.Assert;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author twg
 * @since 2018-05-30
 */
@Service
public class SysRoleServiceImpl extends ServiceImpl<SysRoleMapper, SysRole> implements ISysRoleService {
    @Autowired
    private SysRoleMapper roleMapper;
    @Autowired
    private SysRoleDeptMapper roleDeptMapper;
    @Autowired
    private SysUserRoleMapper userRoleMapper;
    @Autowired
    private SysRoleMenuMapper roleMenuMapper;

    @Override
    public List<RoleDTO> findRolesWithDeptByDeptIds(String... deptIds) {
        return null;
    }

    @Override
    public RoleDTO getRoleWithDeptByRoleId(Long roleId) {
        Assert.notNull(roleId, "角色ID不能为空");
        return roleMapper.getRoleWithDeptByRoleId(roleId);
    }

    @Override
    public Boolean insertRole(RoleDTO roleDTO) {
        if (isExist(roleDTO.getRoleName(), roleDTO.getCompId())) {
            throw new BaseException("角色 [" + roleDTO.getRoleName() + "] 已存在");
        }
        SysRole sysRole = new SysRole();
        BeanUtil.copyProperties(roleDTO, sysRole);
        Boolean result = this.insert(sysRole);
        SysRoleDept roleDept = new SysRoleDept();
        roleDept.setRoleId(sysRole.getId());
        roleDept.setDeptId(roleDTO.getDeptId());
        roleDeptMapper.insert(roleDept);
        return result;
    }

    @Override
    @Transactional
    public Boolean updateRole(RoleDTO role) {
        //删除原有的角色部门关系
        SysRoleDept roleDept = new SysRoleDept();
        roleDept.setRoleId(role.getId());
        roleDeptMapper.delete(new EntityWrapper<SysRoleDept>(roleDept));
        //维护角色部门关系
        roleDept.setDeptId(role.getDeptId());
        roleDeptMapper.insert(roleDept);
        SysRole sysRole = new SysRole();
        BeanUtil.copyProperties(role, sysRole);
        return this.updateById(sysRole);
    }

    @Override
    public List<RoleDTO> findByDeptId(Long deptId) {
        return roleMapper.findByDeptId(deptId);
    }

    @Override
    public Page<RoleDTO> findRoleWithDeptByPage(Query query) {
        return query.setRecords(roleMapper.findRoleWithDeptPage(query, query.getCondition()));
    }

    @Override
    public Boolean isExist(String roleName, Long compId) {
        Assert.notBlank(roleName, "角色名称不为空");
        Assert.notNull(compId, "公司ID不能为空");
        SysRole role = new SysRole();
        role.setRoleName(roleName);
        role.setCompId(compId);
        SysRole r = roleMapper.selectOne(role);
        if (r == null) {
            return Boolean.FALSE;
        }
        return Boolean.TRUE;
    }

    @Override
    @Transactional
    public Boolean deleteRoleById(Long id) {
        SysUserRole userRole = new SysUserRole();
        userRole.setRoleId(id);
        if (userRoleMapper.selectCount(new EntityWrapper<>(userRole)) > 0) {
            throw new BaseException("该角色已绑定用户，不能删除");
        }
        SysRoleDept roleDept = new SysRoleDept();
        roleDept.setRoleId(id);
        roleDeptMapper.delete(new EntityWrapper<>(roleDept));
        userRoleMapper.delete(new EntityWrapper<>(userRole));
        SysRoleMenu roleMenu = new SysRoleMenu();
        roleMenu.setRoleId(id);
        roleMenuMapper.delete(new EntityWrapper<>(roleMenu));
        return Boolean.TRUE;
    }
}
